﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Innoapps.FileTransfer.Helpers
{
    public class StreamHelper
    {
        /// <summary>
        /// Gets the checksum for a stream using a buffered stream for memory optimisation
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string GetChecksumBuffered(Stream stream)
        {

            if (stream == null)
                return "";

            using (var bufferedStream = new BufferedStream(stream, 1024 * 32))
            {
                var sha = new SHA256Managed();
                byte[] checksum = sha.ComputeHash(bufferedStream);
                return BitConverter.ToString(checksum).Replace("-", String.Empty);
            }
        }

    }
}
